<?php
/**
 * 用户控制器
 * Created by Forever.
 * User: Forever
 * Date: 2018/10/03
 * Time: 上午 10:39
 */
namespace app\api\controller;
use think\Controller;
use think\Request;
use \think\Db;
use think\Validate;
class User extends Base{
    protected $key = "y9IGuCNKquRY1p9mL2gldQacRdQcNx4x";
	public function _initialize() {
	  parent::_initialize ();
	}


   //例子
	public function index(){ 
		//获取所有输入的数据
		$data = $this->data;
		//验证输入的信息
        $check = $this->validate($data,'User.detail');
        if($check !==true){
           return api_return($check);
        }
        //model处理信息
        $result = model('User')->getDetail($data);
        if($result != false ){
            return api_return(10002,$result);
        }else{
            return api_return(-10002);
        }
	}

    /*
    * 获取角色列表
    */
    public function get_role_list(){
        $data = $this->data;
        $result = model('User')->getRoleList($data);
        if($result != false ){
            return api_return(10002,$result);
        }else{
            return api_return(-10002);
        }
    }

   /*
    * 编辑角色信息
    */
    public function edit_role(){
        $data = $this->data;
        $check = $this->validate($data,'User.role_edit');
        if($check !==true){
           return api_return($check);
        }
        $result = model('User')->editRole($data);
        if($result !== false ){
            return api_return(10008,$result);
        }else{
            return api_return(-10013);
        }
    }


    /*
    * 前端用户个人中心
    */
    public function user_center(){
      $data = $this->data;
      $check = $this->validate($data,'User.user_center');
      if($check !==true){
         return api_return($check);
      }

      $result = model('User')->getUserDetail($data);
      if($result !== false && $result >0){
        return api_return(10002,$result);
      }else{
        return api_return($result);
      }
    }


    /*
    *获取角色列表
    */
    public function editRole(){
      $data = $this->data;
      $check = $this->validate($data,'User.validate');
      if($check !==true){
         return api_return($check);
      }
      $result = model('User')->editUserRole($data);
      if($result !== false && $result >0){
        return api_return(10008);
      }else{
        return api_return($result);
      }
    }

  /*
  *获取角色列表
  */
    // public function role_list(){
    //   $data = $that->data;
    //   $check = $this->validate($data,'User.role');
    //   if($check !==true){
    //      return api_return($check);
    //   }
    //   $result = model('User')->getRoleList($data);
    //   if($result !== false && $result >0){
    //     return api_return(10002,$result);
    //   }else{
    //     return api_return($result);
    //   }

    // }

   /*
    * 获取定位
    */
    public function get_localtion(){
       $data = $this->data;
       $prepAddr = $data['addr'];
       $result = $this->getLatng($prepAddr);
       return api_return(10002,$result);
       // $url = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=".$data['latitude'].",".$data['longitude']."&destinations=".$latng['lat'].",".$latng['lng']."&ak=".$this->key;
       // $geocode = file_get_contents($url);
       // $output= json_decode($geocode,true);
       // echo json_encode($output);
    }

    /**
    * 根据地址获取经纬度
    *
    */
     public function getLatng($prepAddr){
        $geocode=file_get_contents("http://api.map.baidu.com/geocoder/v2/?address=".$prepAddr."&output=json&ak=".$this->key);
        $output = json_decode($geocode,true);
        if(!empty($output['result'])){
          $latng = $output['result']['location'];
          return $latng;
        }else{
          return $output;
        }

    }



}
